#
#  Copyright 2008 Garth Williams
#
#  This file is part of CommandPost.
#
#  CommandPost is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  CommandPost is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CommandPost.  If not, see <http://www.gnu.org/licenses/>.
#

class Report < ActiveRecord::Base
  belongs_to :stage
  belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_user_id'
  belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
  belongs_to :milestone
  belongs_to :priority
  belongs_to :first_stage, :class_name => 'Stage', :foreign_key=> 'first_stage_id'

  validates_presence_of :title
  validates_existence_of :stage
  validates_existence_of :assigned_to, :allow_nil => true
  validates_existence_of :milestone, :allow_nil => true
  validates_existence_of :priority
  validates_existence_of :created_by
  validates_existence_of :first_stage, :allow_nil => true

  acts_as_paranoid_versioned
  
  def check_source_bindings(commits)
    if (source_bindings.length == 0)
      #no bindings, so no commits allowed
      return commits
    end
    allowed_commits = Array.new
    source_bindings.each do | binding |
      allowed_commits = allowed_commits | binding.check_path(commits)
    end
    #just return the commits that are not allowed
    commits - allowed_commits
  end
end
